const http = require("http");
const url = require("url");
const fs = require("fs");

const server = http
  .createServer((req, res) => {
    const { pathname, query } = url.parse(req.url);

    if (pathname === "/upload" && req.method === "POST") {
      const fileWriter = fs.createWriteStream("./foo.png", { flag: "a+" });
      req.on("data", (data) => {
        console.log(data);
        fileWriter.write(data);
      });

      res.on("end", () => {
        console.log("文件上传成功");
        res.end("文件上传成功");
      });
    }
  })
  .listen(8000, () => {
    console.log("文件上传服务器开启成功");
  });
